package com.finalab.yueqiu.common;

import com.finalab.yueqiu.constant.ResponseCode;
import com.finalab.yueqiu.exception.*;
import com.finalab.yueqiu.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * 统一异常处理
 * @author 莉安
 */
@Controller
public class ExceptionResolver implements HandlerExceptionResolver{
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        PrintWriter out = null;
        try {
            out = httpServletResponse.getWriter();
            if(e != null){
                ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR.getValue());
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }finally {
            if(out != null){
                out.close();
            }
        }

        return null;
    }
}
